from sqlalchemy.orm import Mapped, relationship

from .base import Base, str_50


class Author(Base):
    __tablename__ = 'authors'

    first_name: Mapped[str_50]
    last_name: Mapped[str_50]

    def __repr__(self):
        return f"{self.first_name} {self.last_name}"

    def __getitem__(self, item):
        return getattr(self, item)

    def to_json(self):
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}

    books = relationship(
        "Book", backref="author", lazy="joined", cascade="all, delete-orphan")
